package com.dream.common.enums;

import com.alibaba.fastjson.JSONObject;
import com.dream.common.constans.RedisKeysDefine;
import com.dream.common.utils.JsonUtils;
import com.dream.common.utils.payutils.bean.MessageBeanOld;
import com.dream.common.utils.payutils.bean.configuration.FunctionSwitchBean;
import com.dream.common.utils.pickutils.NumberUtil;
import com.dream.dto.request.GroupMemberBatchRequest;
import com.dream.dto.response.GroupMemberRedPack;
import com.dream.dto.response.GroupMemberRedPackPicker;
import com.dream.dto.socket.SocketBean;
import com.dream.entity.GroupRules;
import com.dream.net.socket.base.BaseSocketController;
import com.dream.service.basic.BaseServiceHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;


public enum CustomMsgEnum {
    //自定义文本

    CHA_WO("查我", "这里主要描述用途{}") {
        @Override
        public void resolve(MessageBeanOld messageBean) {
            logger.info("========== tag :{}  messageBean: {}", tag, messageBean);
            SocketBean socketBean = new SocketBean();
            socketBean.command = 1;
            String userName = messageBean.userName;
            MessageBeanOld respondMsgBean = getMessageBeanInfo(messageBean);
            //用户登录即调用获取规则,groupRules、functionSwitchBean不存在为空,严谨处理判断空
            GroupRules groupRules = BaseServiceHelper.getRedisCacheService().getObject(RedisKeysDefine.CALC_RULE.concat(messageBean.userName), GroupRules.class);
            GroupMemberBatchRequest batchRequest = BaseServiceHelper.getProcedureServiceImpl().getMemberRoles(userName);
            FunctionSwitchBean functionSwitchBean = JsonUtils.parseObject(groupRules.getFeatureSwitch(), FunctionSwitchBean.class);
            if (functionSwitchBean != null) {
                //玩家自由查流水打开或者管理员查
                if (functionSwitchBean.flowwater
                        || (batchRequest.roleMap.get("managerList") != null && batchRequest.roleMap.get("managerList").contains(messageBean.sendMessageUserId))) {
                    GroupMemberRedPack groupMemberRedPackInfo = BaseServiceHelper.getRedPackageSendMapper().selectGroupMemberRedPackInfo(messageBean.userName, messageBean.sendMessageUserId);
                    GroupMemberRedPackPicker groupMemberRedPackPicker = BaseServiceHelper.getRedPackagePickMapper().selectPickUserByUserID(messageBean.userName, messageBean.sendMessageUserId);
                    respondMsgBean.sendMsg = messageBean.sendMessageUserName
                            + "\n伐包个数：" + groupMemberRedPackInfo.sendNumber
                            + "\n伐包琻额：" + groupMemberRedPackInfo.sendMoney
                            + "\n枪包个数：" + groupMemberRedPackPicker.pickNumber
                            + "\n枪包琻额：" + groupMemberRedPackPicker.pickMoney
                            + "\n奖励琻额：" + groupMemberRedPackPicker.rewardsMoney
                            + "\n中蕾琻额：" + groupMemberRedPackInfo.thunderMoney
                            + "\n输赢琻额：" + NumberUtil.add(NumberUtil.add(groupMemberRedPackInfo.profitMoney, groupMemberRedPackPicker.rewardsMoney), groupMemberRedPackPicker.pickMoney);
                } else {
                    respondMsgBean.sendMsg = "当前不允许玩家查流水，可通知管理员开启玩家查流水"
                            + "\n1，登陆云算法官网"
                            + "\n2，打开功能开关界面"
                            + "\n3，打开玩家自由查流水";
                }
            } else {
                respondMsgBean.sendMsg = "未设置功能!";
            }
            socketBean.data = JsonUtils.toJSONString(respondMsgBean);
            if (!StringUtils.isEmpty(respondMsgBean.sendMsg)) {
                //用户登录即调用获取规则,groupRules、functionSwitchBean不存在为空,严谨处理判断空
                BaseServiceHelper.getSocketController().sendCommand(userName, socketBean);
                BaseServiceHelper.getAsyncInvokeService().msgUnityWrite(userName, socketBean.data);
                BaseServiceHelper.getConsoleWebSocket().sendMessageToUser(userName, JsonUtils.toJSONString(socketBean));
            }
        }
    },
    CHA("查", "这里主要描述用途{}") {
        @Override
        public void resolve(MessageBeanOld messageBean) {
            logger.info("========== tag :{}  messageBean: {}", tag, messageBean);
            String userName = messageBean.userName;
            MessageBeanOld respondMsgBean = null;
            SocketBean socketBean = null;
            GroupMemberBatchRequest batchRequest = BaseServiceHelper.getProcedureServiceImpl().getMemberRoles(userName);
            if (batchRequest.roleMap.get("managerList") != null && batchRequest.roleMap.get("managerList").contains(messageBean.sendMessageUserId)) {
                if (messageBean.atList != null && messageBean.atList.size() > 0) {
                    socketBean = new SocketBean();
                    socketBean.command = 1;
                    respondMsgBean = getMessageBeanInfo(messageBean);
                    GroupMemberRedPack groupMemberRedPackInfo = BaseServiceHelper.getRedPackageSendMapper().selectGroupMemberRedPackInfo(messageBean.userName, messageBean.atList.get(0).atUserId);
                    GroupMemberRedPackPicker groupMemberRedPackPicker = BaseServiceHelper.getRedPackagePickMapper().selectPickUserByUserID(messageBean.userName, messageBean.atList.get(0).atUserId);
                    respondMsgBean.sendMsg = messageBean.atList.get(0).atUserName
                            + "\n伐包个数：" + groupMemberRedPackInfo.sendNumber
                            + "\n伐包琻额：" + groupMemberRedPackInfo.sendMoney
                            + "\n枪包个数：" + groupMemberRedPackPicker.pickNumber
                            + "\n枪包琻额：" + groupMemberRedPackPicker.pickMoney
                            + "\n奖励琻额：" + groupMemberRedPackPicker.rewardsMoney
                            + "\n中蕾琻额：" + groupMemberRedPackInfo.thunderMoney
                            + "\n输赢琻额：" + NumberUtil.add(NumberUtil.add(groupMemberRedPackInfo.profitMoney, groupMemberRedPackPicker.rewardsMoney), groupMemberRedPackPicker.pickMoney);
                    socketBean.data = JsonUtils.toJSONString(respondMsgBean);
                }
            }
            if (respondMsgBean != null && !StringUtils.isEmpty(respondMsgBean.sendMsg)) {
                //用户登录即调用获取规则,groupRules、functionSwitchBean不存在为空,严谨处理判断空
                BaseServiceHelper.getSocketController().sendCommand(userName, socketBean);
                BaseServiceHelper.getAsyncInvokeService().msgUnityWrite(userName, socketBean.data);
                BaseServiceHelper.getConsoleWebSocket().sendMessageToUser(userName, JsonUtils.toJSONString(socketBean));
            }
        }
    },
    ;
    public String tag;
    public String description;

    CustomMsgEnum(String tag, String description) {
        this.tag = tag;
        this.description = description;
    }


    public void resolve(MessageBeanOld tag) {
        System.out.println("无匹配tag----");
    }

    public static CustomMsgEnum getEnumByTag(String tag) {
        for (CustomMsgEnum operationContent : values()) {
            if (operationContent.tag.equals(tag)) {
                return operationContent;
            }
        }
        return null;
    }


    private static Logger logger = LoggerFactory.getLogger(BaseSocketController.class);

    public MessageBeanOld getMessageBeanInfo(MessageBeanOld messageBean) {
        String info = BaseServiceHelper.getRedisCacheService().get(RedisKeysDefine.APP_USER_INFO.concat(messageBean.userName));
        JSONObject jsonObject = JSONObject.parseObject(info);
        MessageBeanOld respondMsgBean = new MessageBeanOld();
        respondMsgBean.userName = messageBean.userName;
        respondMsgBean.groupId = messageBean.groupId;
        respondMsgBean.groupName = messageBean.groupName;
        respondMsgBean.sendMessageTime = System.currentTimeMillis();
        respondMsgBean.sendMessageUserId = jsonObject.getString("appUserId");
        respondMsgBean.sendMessageUserName = jsonObject.getString("appUserName");
        respondMsgBean.sendMessageUserImg = jsonObject.getString("appUserHeadImg");
        respondMsgBean.messageType = 2;
        return respondMsgBean;
    }
}
